/**
 * @description 自定义公共Modal工具
 */

import ConfirmDialog from './components/confirmDialog.vue'
const Modal = {}

Modal.install = function (Vue) {
  Vue.prototype.$modal = {
    /**
     * @param {title: '标题', msg: '内容', sureTitle: '确定', cancelTitle: '取消'}
     */
    confirm(options) {
      const Instance = Vue.extend(ConfirmDialog)
      let currentObj = new Instance()
      if (typeof options === 'string') {
        currentObj.options = {
          msg: options,
        }
      } else {
        currentObj.options = options
      }
      let objEl = currentObj.$mount().$el
      document.body.appendChild(objEl)
      return currentObj
        .show()
        .then((val) => {
          currentObj = null
          return Promise.resolve(val)
        })
        .catch((err) => {
          currentObj = null
          return Promise.reject(err)
        })
    },
  }
}

export default Modal
